package sky.wicket;

import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import sky.domain.Airport;
import sky.service.AirportService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sky.service.CityService;

/**
 * Created with IntelliJ IDEA.
 * User: Gauss
 * Date: 15.09.13
 * Time: 21:19
 * To change this template use File | Settings | File Templates.
 */
public class AddUpdateAirport extends BasePage {

    @SpringBean
    private AirportService airportService;
    @SpringBean
    private CityService cityService;

    private Airport airport;
    private boolean createFlag = false;
    private boolean updateFlag = false;
    static final Logger log = LoggerFactory.getLogger(AddUpdateAirport.class);

    public AddUpdateAirport() {
        airport = new Airport();
        createFlag = true;
        addElementsToPage();
    }

    public AddUpdateAirport(String airportName) {
        this.airport = airportService.getAirportByName(airportName);
        updateFlag = true;
        addElementsToPage();
    }

    private void addElementsToPage() {
        Form<Airport> newAirportForm = new Form<Airport>("newAirportForm", new CompoundPropertyModel<Airport>(airport)) {

            @Override
            protected void onSubmit() {
                log.info("Submitting Form...");
                log.info("Information From Form:");
                log.info("   Airport's Name: " + airport.getName());
                log.info("   Airport's City Name: " + airport.getCity().getName());
                log.info("   Airport's Code: " + airport.getCode());
                if (cityService.getCityByName(airport.getCity().getName())!=null) {
                    //airportService.addAirport(airport);
                    log.info("Form Submitted");
                    createOrUpdate();
                } else {
                    error("Cannot submit form");
                }
            }

            @Override
            protected void onError() {}
        };
        newAirportForm.add(new TextField<String>("name").setRequired(true));
        newAirportForm.add(new TextField<String>("city.name").setRequired(true));
        newAirportForm.add(new TextField<String>("code").setRequired(true));
        add(newAirportForm);
        add(new FeedbackPanel("feedback"));
    }

    private void createOrUpdate() {
        log.info("Checking Flags...");
        log.info("Create Flag: " + createFlag);
        log.info("Update Flag: " + updateFlag);
        if (createFlag) {
            airportService.addAirport(airport);
            return;
        }
        if (updateFlag) {
            airportService.editAirport(airport);
            return;
        }
    }
}
